const router = require("koa-router")();
const MembersModel = require("../../models/Members")
// 查询数据库
const upload = require("../../config/multer")
/* const multer = require('koa-multer')
const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, `${process.cwd()}/static`)
    },
    filename: function (req, file, cb) {
        let type = file.originalname.split('.')[1]
        cb(null, `${file.fieldname}-${Date.now().toString(16)}.${type}`)
    }
})
//加载配置
var upload = multer({
    storage: storage
}); */  //config/multer.js
router.post('/m1/doAdd',upload.single('file'), async (ctx, next) => {
    console.log(ctx.req.body)
    console.log(ctx.req.file.filename)
    var {name,age,like,friend} = ctx.req.body;
    var avatar = ctx.req.file.filename;
    var host = ctx.request.header.host;
    avatar = `http://${host}/${avatar}`
    var data = new MembersModel({
        name,
        age,
        like:Boolean(like),
        friend:Boolean(friend),
        avatar
    });
    data.save(err=>{
        if(err){throw err};
    })
    ctx.redirect("/m1")
}) 
module.exports = router;